package com.acoffee.web;

import com.acoffee.mapper.GoodsMapper;
import com.acoffee.pojo.Goods;
import com.acoffee.pojo.PageBean;
import com.acoffee.service.GoodsService;
import com.acoffee.service.Impl.GoodsServiceImpl;
import com.acoffee.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author acoffee
 * @create 2021-10-29 11:19
 */
@WebServlet("/goods/*")
public class GoodsServlet extends BaseServlet {

    private GoodsService goodsService = new GoodsServiceImpl();

    public void queryNewGoods(HttpServletRequest req, HttpServletResponse resp){
        List<Goods> goods = goodsService.queryGoodsIsNew();
        response(resp,goods);
    }

    public void queryHotGoods(HttpServletRequest req, HttpServletResponse resp){
        List<Goods> goods = goodsService.queryGoodsIsHot();
        response(resp,goods);
    }

    public void details(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String id = req.getParameter("id");
        Goods goods = goodsService.details(Integer.parseInt(id));
        req.setAttribute("goods",goods);
        req.getRequestDispatcher("/pages/details.jsp").forward(req,resp);
    }

    public void queryPageByCid(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String cid = req.getParameter("cid");
        String currentPage = req.getParameter("currentPage");

        PageBean<Goods> pageBean = goodsService.queryPageByCid(Integer.parseInt(cid), Integer.parseInt(currentPage));

        req.setAttribute("pageBean",pageBean);
        req.getRequestDispatcher("/pages/show_list.jsp").forward(req,resp);
    }

    public void queryRankGoods(HttpServletRequest req, HttpServletResponse resp){
        String cid = req.getParameter("cid");

        List<Goods> rankGoods = goodsService.queryRankGoods(Integer.parseInt(cid));
        System.out.println(rankGoods);
        response(resp,rankGoods);
    }
}
